﻿#include "injectoradddialog.h"

InjectorAddDialog::InjectorAddDialog()
{
    setup_ui();
    connect(okButton, &QAbstractButton::clicked, this, &InjectorAddDialog::slt_okButton);
    connect(cancelButton, &QAbstractButton::clicked, this, &QDialog::reject);
}

InjectorAddDialog::~InjectorAddDialog()
{

}

InjectorData InjectorAddDialog::getInjData()
{
    InjectorData data;
    data.name = name_ed->text();
    data.position = pos_ed->text();
    data.status = 0;
    data.settings.inject_period = inj_period_ed->text().toInt();
    data.settings.comm_period = comm_period_ed->text().toInt();
    data.settings.inject_volume = inj_vol_ed->text().toInt();
    data.settings.max_current = max_cur_ed->text().toInt();
    data.settings.min_volt = min_vol_ed->text().toInt();
    return data;
}

void InjectorAddDialog::slt_okButton()
{
    if(name_ed->text().size() == 0 || pos_ed->text().size() == 0)
        QMessageBox::warning(NULL,  QStringLiteral("字符串不合法"),
                              QStringLiteral("不含空格和特殊符号,./:'等，不能为空"));
    else
        accept();
}

void InjectorAddDialog::setup_ui()
{
    QGridLayout* layout = new QGridLayout();

    QLabel* name_l = new QLabel(QStringLiteral("名称"));
    name_l->setAlignment(Qt::AlignCenter);
    name_l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    QLabel* pos_l = new QLabel(QStringLiteral("位置"));
    pos_l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    pos_l->setAlignment(Qt::AlignCenter);
    name_ed = new QLineEdit();
    name_ed->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    pos_ed = new QLineEdit();
    pos_ed->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QLabel* inj_period_l = new QLabel(QStringLiteral("注脂周期(h)"));
    inj_period_l->setAlignment(Qt::AlignCenter);
    inj_period_ed = new QLineEdit();
    QLabel* comm_period_l = new QLabel(QStringLiteral("通信周期(h)"));
    comm_period_l->setAlignment(Qt::AlignCenter);
    comm_period_ed = new QLineEdit();
    QLabel* inj_vol_l = new QLabel(QStringLiteral("注脂量(ml)"));
    inj_vol_l->setAlignment(Qt::AlignCenter);
    inj_vol_ed = new QLineEdit();
    QLabel* max_cur_l = new QLabel(QStringLiteral("电流上限(mA)"));
    max_cur_l->setAlignment(Qt::AlignCenter);
    max_cur_ed = new QLineEdit();
    QLabel* min_vol_l = new QLabel(QStringLiteral("电压下限值(mV)"));
    min_vol_l->setAlignment(Qt::AlignCenter);
    min_vol_ed = new QLineEdit();


    okButton = new QPushButton(QStringLiteral("确认"));
    okButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    cancelButton = new QPushButton(QStringLiteral("取消"));
    cancelButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    layout->addWidget(name_l,0,0,1,2);
    layout->addWidget(name_ed,0,2,1,4);
    layout->addWidget(pos_l,1,0,1,2);
    layout->addWidget(pos_ed,1,2,1,4);
    layout->addWidget(inj_period_l,2,0,1,2);
    layout->addWidget(inj_period_ed,2,2,1,4);
    layout->addWidget(comm_period_l,3,0,1,2);
    layout->addWidget(comm_period_ed,3,2,1,4);
    layout->addWidget(inj_vol_l,4,0,1,2);
    layout->addWidget(inj_vol_ed,4,2,1,4);
    layout->addWidget(max_cur_l,5,0,1,2);
    layout->addWidget(max_cur_ed,5,2,1,4);
    layout->addWidget(min_vol_l,6,0,1,2);
    layout->addWidget(min_vol_ed,6,2,1,4);
    layout->addWidget(okButton,7,0,1,3);
    layout->addWidget(cancelButton,7,3,1,3);

    setLayout(layout);
    setWindowTitle(QStringLiteral("新增注脂器设备"));
    setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::Drawer);
}

